require(['./config'],()=>{
	require(['template','footer'],(template,header)=>{
		class cartsList {
			constructor() {
				this.render()
				this.calcTotalMoney()
				this.checksChange()
				this.setAllCheckStatus()
				this.setAllCheck()
				this.decrease()
				this.delete()
				this.xjPrice()
			}
			render(){
			  //localstorage
			  const cart = localStorage.getItem('cart')
			  if(cart){
				this.cart = JSON.parse(cart) //方便其它方法使用
			    $('#center-content').html(template('cartTemplate',{cart:this.cart})) 
			  }else{
				  //没有数据，显示
				$('.main').hide()
				$('.cart-empty').show()
			  }
			}
			calcTotalMoney (){
				//从0算总价 ;默认总价
				 let totalMoney = this.cart.reduce((total,shop)=>{
					 if(shop.checked) total += shop.price * shop.count
					 return total
				 },0)
				 $('#goods-zj').html(totalMoney.toFixed(2))
				 //判断选中数量
				 let  totalNum = this.cart.reduce((totalNum,shop)=>{
					 if(shop.checked){
						 totalNum +=shop.count
					 }
					 return totalNum
				 },0)
				 $('.keyValue').html(totalNum)			
			}
			xjPrice(){
				let xj = this.cart.forEach((xj,shop)=>{
					if(shop.checked){
						xj = shop.price * shop.count
					} 
					return xj
				},0)
				$('.xj').html(xj)
			}
			checksChange(){
				const _this = this
				//所有单选绑定事件，重新计算总价
				$('.checks').on('change',function(){
					//获取当前数据id
					const id = $(this).parents('.allselect').data('id')
					//修改当前的checked属性为当前选框状态:内外层this都要
                     _this.cart.map(shop => {
						 if(shop.id===id) shop.checked = $(this).prop('checked')
						 return shop
					 })
					 //改localstorage里的状态:重新更新localstorage的数据
					 localStorage.setItem('cart',JSON.stringify(_this.cart))
					 _this.calcTotalMoney()
					 _this.setAllCheckStatus()
					 _this.setAllCheck()
				})
			}
			setAllCheckStatus(){
					 //单选
					 const isAllCheck = this.cart.every(shop=> shop.checked)
					 $('#allCheck').prop('checked',isAllCheck)
			}
			setAllCheck(){
				   //全选
				  const _this = this
				     $('#allCheck').on('click',function(){
						 $('.checks').prop('checked',$(this).prop('checked'))
						 $('.checks').change()
					   _this.calcTotalMoney()

					// 	   const allChecked = $(this).prop('checked')
					// 	   $('.checks').prop('checked',allChecked)
					 })
					
			}
			decrease(){
				const _this=this
				$('.decrease').on('click',function(){
					if($(this).next().val()>1){
						var num = $(this).next().val()-1
						$(this).next().val(num)
						//获取当前商品的id
						const id= $(this).parents('.father').data('id')
						_this.cart = _this.cart.map(shop=>{
							var xj = shop.price*shop.count
							$('.xj em').html(xj)
							if(shop.id === id){
								shop.count--
							}
							return shop
						})
						localStorage.setItem('cart',JSON.stringify(_this.cart))
						_this.calcTotalMoney ()
						_this.xjPrice()
					}else{
						$('.decrease').attr('disabled','disabled')
						}
						return false
					})
					$('.increase').on('click',function(){
					  var num = parseInt($(this).prev().val())+1
					  $(this).prev().val(num)
					  const id = $(this).parents('.father').data('id')
					  _this.cart = _this.cart.map(shop =>{
						  if(shop.id === id){
							  shop.count ++
						  }
						  return shop
					  })
						localStorage.setItem('cart',JSON.stringify(_this.cart))
						_this.calcTotalMoney()
						_this.xjPrice()
		
						return false
					})
			}
			delete(){
				 const  _this=this
				$('.del').on('click',function(){
					$(this).parents('.father').remove()
					//获取当前商品id
					const id = $(this).parents(".father").data(id).id
					console.log(id)
					_this.cart = _this.cart.filter(shop=>{
						return id != shop.id
					})
					localStorage.setItem('cart',JSON.stringify(_this.cart))
					return false
				})
				
			}
		}
		
		new cartsList()
	})
})